1 //==============================================================================
2 // file : IndexFiles.java
3 // project: Lucene Search System
4 //
5 // last change: date: $Date: 2003/09/09 03:11:52 $
6 // by: $Author: bitiboy $
7 // revision: $Revision: 1.1 $
8 //------------------------------------------------------------------------------
9 // copyright: GNU GPL Software License (see class documentation)
10 //==============================================================================
11
12 package com.justhis.lucene.xml;
13
14
15 /*
16 * $Id: IndexFiles.java,v 1.1 2003/09/09 03:11:52 bitiboy Exp $
17 *
18 * Copyright 2003 Acai Software All Rights Reserved.
19 *
20 * This file LuceneException.java is part of the Lucene Search System.
21
22 * The Lucene Search System is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26
27 * Lucene Search System is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31
32 * You should have received a copy of the GNU General Public License
33 * along with the Lucene Search System; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
36 * http://www.justhis.com http://ejb.cn
37 * CONTACT: email = webmaster@justhis.com superaxis@sohu.com
38 */
39 import com.justhis.lucene.LuceneException;
40
41 import org.apache.lucene.analysis.standard.StandardAnalyzer;
42 import org.apache.lucene.document.Document;
43 import org.apache.lucene.index.IndexReader;
44 import org.apache.lucene.index.IndexWriter;
45
46 import org.xml.sax.SAXException;
47
48 import java.io.File;
49 import java.io.IOException;
50
51 import javax.xml.parsers.ParserConfigurationException;
52
53
54 /***
55 * create index from xml files
56 *
57 * @author <a href="http://blog.ejb.cn">acai</a>
58 * @version $Revision: 1.1 $
59 */
60 public class IndexFiles {
61 //~ Methods ----------------------------------------------------------------
62
63 /***
64 * ??????????????
65 *
66 * @param writer lucene writer
67 * @param file ????????????
68 *
69 * @throws ParserConfigurationException ??????xml??????????????????????
70 * @throws SAXException ????sax????xml????????
71 * @throws IOException ????????????????
72 */
73 public static void indexDocs(IndexWriter writer, File file)
74 throws ParserConfigurationException, SAXException,
75 IOException {
76 if (file.isDirectory()) {
77 String[] files = file.list();
78
79 for (int i = 0; i < files.length; i++) {
80 indexDocs(writer, new File(file, files[i]));
81 }
82 } else {
83 System.out.println("adding " + file);
84
85 XMLDocumentHandlerSAX hdlr = new XMLDocumentHandlerSAX(file);
86 writer.addDocument(hdlr.getDocument());
87
88 // For DOM, use
89 // XMLDocumentHandlerDOM hdlr = new XMLDocumentHandlerDOM();
90 // writer.addDocument(hdlr.createXMLDocument(file));
91 }
92 }
93
94 /***
95 * ??????????????????
96 *
97 * @param indexPath ????????????????
98 * @param filePath ????????
99 *
100 * @throws LuceneException ??????????XML??????????????.
101 */
102 public static void addDocument(String indexPath, String filePath)
103 throws LuceneException {
104 IndexWriter writer = null;
105
106 try {
107 if (IndexReader.indexExists(indexPath)) {
108 writer = new IndexWriter(indexPath, new StandardAnalyzer(),
109 false
110 );
111 } else {
112 writer = new IndexWriter(indexPath, new StandardAnalyzer(), true);
113 }
114
115 indexDocs(writer, new File(filePath));
116 writer.optimize();
117 } catch (ParserConfigurationException e) {
118 throw new LuceneException(e.getMessage(), e);
119 } catch (SAXException e) {
120 throw new LuceneException(e.getMessage(), e);
121 } catch (IOException e) {
122 throw new LuceneException(e.getMessage(), e);
123 } finally {
124 if (writer != null) {
125 try {
126 writer.close();
127 } catch (IOException e1) {
128 }
129 }
130 }
131 }
132
133 /***
134 * ??????????????????
135 *
136 * @param indexPath ????????
137 * @param keyId ?????? ??????????????????objectid??????????
138 *
139 * @throws LuceneException ??????????????????IO????
140 */
141 public static void deleteDocument(String indexPath, String keyId)
142 throws LuceneException {
143 IndexReader reader = null;
144
145 try {
146 reader = IndexReader.open(indexPath);
147
148 for (int i = 0; i < reader.numDocs(); i++) {
149 if (reader.isDeleted(i)) {
150 continue;
151 }
152
153 Document doc = reader.document(i);
154
155 if (doc.get("objectId").equals(keyId)) {
156 reader.delete(i);
157
158 break;
159 }
160 }
161 } catch (IOException e) {
162 throw new LuceneException(e.getMessage(), e);
163 } finally {
164 if (reader != null) {
165 try {
166 reader.close();
167 } catch (IOException e1) {
168 e1.printStackTrace();
169 }
170 }
171 }
172 }
173 }
174 /*
175 * $Log: IndexFiles.java,v $
176 * Revision 1.1 2003/09/09 03:11:52 bitiboy
177 * *** empty log message ***
178 *
179 * Revision 1.1 2003/09/09 00:54:45 bitiboy
180 * *** empty log message ***
181 *
182 * Revision 1.1 2003/09/07 08:23:50 superaxis
183 * *** empty log message ***
184 *
185 *
186 */
This page was automatically generated by Maven